from django.shortcuts import render,redirect,HttpResponse
from django import forms
# Create your views here.

from app01 import models
def Lab(request):
    all_Lab = models.Lab.objects.all()

    return render(request,'Lab_list.html',{'k1':all_Lab})

from django.core.exceptions import ValidationError

class LoginForm(forms.Form):
    def clean_l_name(self):
        # 局部钩子
        # 通过校验   返回当前字段的值
        # 不通过校验   抛出异常
        value = self.cleaned_data.get('l_name')
        if models.Lab.objects.filter(l_name=value):
            raise ValidationError('用户名已存在')
        return value

    l_name = forms.CharField(
        min_length=8,
        required=True,
        label="实验室名",
        validators=[],
        error_messages={
            "required": "不能为空",
            "invalid": "格式错误",
            "min_length": "用户名最短8位"
        }
    )
    room = forms.CharField(

        required=True,
        label="房间号",
        error_messages={
            "required": "不能为空",

        }
    )
    floor = forms.CharField(

        required=True,
        label="楼层",
        error_messages={
            "required": "不能为空",

        }
    )
    leader = forms.CharField(

        required=True,
        label="负责人",
        error_messages={
            "required": "不能为空",

        }
    )



def Lab_add(request):
    if request.method == 'POST':
        form_obj = LoginForm(request.POST)

        if form_obj.is_valid():  # 校验数据
            # 插入数据库
            print(form_obj.cleaned_data)
            print(request.POST)
            l_name = form_obj.cleaned_data['l_name']
            room = form_obj.cleaned_data['room']
            floor = form_obj.cleaned_data['floor']
            leader = form_obj.cleaned_data['leader']
            models.Lab.objects.create(l_name=l_name,room=room,floor=floor,leader_id=leader)

            return redirect('/Lab_list/')
    else:
        print(1)
        form_obj = LoginForm()
    return render(request, 'Lab_add1.html', {'form_obj': form_obj})




def Lab_edit(request):
    error =''
    id = request.GET.get('id')
    # 获取要编辑对象的ID
    Lab = models.Lab.objects.filter(pk=id)
    # 对象列表
    if not Lab:
        return HttpResponse('数据不存在')
    Lab_obj=Lab[0]
    if request.method == 'POST':
    # 获取新提交的数据
        l_name = request.POST.get('l_name')
        floor = request.POST.get('floor')
        room = request.POST.get('room')
        leader = request.POST.get('leader')
        # 对用户提交的数据进行校验
        if not l_name:
            # 出版社名称为空
            error ='实验室名称不能为空'
        elif Lab_obj.l_name!=l_name and models.Lab.objects.filter(l_name=l_name):
            # 出版社名称重复
            error = '名称已存在'
        elif Lab_obj.l_name == l_name and Lab_obj.leader_id == leader and Lab_obj.room == room and Lab_obj.floor == floor:
            error = '数据没有发生修改'
        else:
            # 修改数据
             Lab_obj.l_name = l_name
             Lab_obj.leader_id = leader
             Lab_obj.room = room
             Lab_obj.floor = floor
             Lab_obj.save()  # 将修改提交的数据库
             return redirect('/Lab_list/')
             # 跳转到展示的页面

    return render(request,'Lab_edit.html',{'pub_obj':Lab_obj,'error':error})
    # 返回一个页面进行展示



def Lab_del(request):

    id = request.GET.get('id')

    models.Lab.objects.filter(pk=id).delete()

    return HttpResponse(1)








def Assets(request):
    all_Assets= models.Assets.objects.all()

    return render(request,'Assets_list.html',{'k1':all_Assets})

class LoginForm1(forms.Form):
    def clean_c_name(self):
        # 局部钩子
        # 通过校验   返回当前字段的值
        # 不通过校验   抛出异常
        value = self.cleaned_data.get('c_name')
        if models.Assets.objects.filter(c_name=value):
            raise ValidationError('用户名已存在')
        return value

    c_name = forms.CharField(
        min_length=4,
        required=True,
        label="物资名称",
        validators=[],
        error_messages={
            "required": "不能为空",
            "invalid": "格式错误",
            "min_length": "用户名最短4位"
        }
    )
    number = forms.CharField(

        required=True,
        label="物资编号",
        error_messages={
            "required": "不能为空",

        }
    )
    buy_time = forms.CharField(

        required=True,
        label="购买时间",
        error_messages={
            "required": "不能为空",

        }
    )
    laboratory = forms.CharField(

        required=True,
        label="所属实验室",
        error_messages={
            "required": "不能为空",

        }
    )
    buy_user = forms.CharField(

        required=True,
        label="购买人",
        error_messages={
            "required": "不能为空",

        }
    )



def Assets_add(request):
    if request.method == 'POST':
        form_obj = LoginForm1(request.POST)

        if form_obj.is_valid():  # 校验数据
            # 插入数据库
            print(form_obj.cleaned_data)
            print(request.POST)
            c_name = form_obj.cleaned_data['c_name']
            number = form_obj.cleaned_data['number']
            buy_time = form_obj.cleaned_data['buy_time']
            laboratory = form_obj.cleaned_data['laboratory']
            buy_user = form_obj.cleaned_data['buy_user']
            models.Assets.objects.create(c_name=c_name,number=number,buy_time=buy_time,laboratory_id=laboratory,buy_user_id=buy_user)

            return redirect('/Assets_list/')
    else:
        form_obj = LoginForm1()
    return render(request, 'Assets_add.html', {'form_obj': form_obj})


def Assets_edit(request):
    error = ''
    id = request.GET.get('id')
    # 获取要编辑对象的ID
    Assets = models.Assets.objects.filter(pk=id)
    # 对象列表
    if not Assets:
        return HttpResponse('数据不存在')
    Assets_obj = Assets[0]
    if request.method == 'POST':
        # 获取新提交的数据
        c_name = request.POST.get('c_name')
        number = request.POST.get('number')
        buy_time = request.POST.get('buy_time')
        laboratory = request.POST.get('laboratory')
        # 对用户提交的数据进行校验
        if not c_name:
            # 出版社名称为空
            error = '请输入用户名'
        elif Assets_obj.c_name != c_name and models.Assets.objects.filter(c_name=c_name):
            # 出版社名称重复
            error = '名称已存在'
        elif Assets_obj.c_name == c_name and Assets_obj.number == number and Assets_obj.buy_time == buy_time and Assets_obj.laboratory_id == laboratory:
            error = '数据没有发生修改'
        else:
            # 修改数据
            Assets_obj.c_name = c_name
            Assets_obj.number = number
            Assets_obj.buy_time = buy_time
            Assets_obj.laboratory_id = laboratory
            Assets_obj.save()  # 将修改提交的数据库
            return redirect('/Assets_list/')
            # 跳转到展示的页面

    return render(request, 'Assets_edit.html', {'Assets_obj': Assets_obj, 'error': error})


def Assets_del(request):

    id = request.GET.get('id')

    models.Assets.objects.filter(pk=id).delete()

    return HttpResponse(1)






def User(request):
    all_User= models.User.objects.all()

    return render(request,'User_list.html',{'k1':all_User})


def User_edit(request):
    error =''
    id = request.GET.get('id')
    # 获取要编辑对象的ID
    User = models.User.objects.filter(pk=id)
    # 对象列表
    if not User:
        return HttpResponse('数据不存在')
    User_obj=User[0]
    if request.method == 'POST':
    # 获取新提交的数据
        username = request.POST.get('username')
        phone = request.POST.get('phone')
        name = request.POST.get('name')
        # 对用户提交的数据进行校验
        if not username:
            # 出版社名称为空
            error ='请输入用户名'
        elif User_obj.username!=username and models.User.objects.filter(username=username):
            # 出版社名称重复
            error = '名称已存在'
        elif User_obj.username == username and User_obj.phone == phone and User_obj.name == name:
            error = '数据没有发生修改'
        else:
            # 修改数据
             User_obj.username = username
             User_obj.phone = phone
             User_obj.name = name
             User_obj.save()  # 将修改提交的数据库
             return redirect('/User_list/')
             # 跳转到展示的页面

    return render(request,'User_edit.html',{'User_obj':User_obj,'error':error})




class LoginForm2(forms.Form):
    def clean_username(self):
        # 局部钩子
        # 通过校验   返回当前字段的值
        # 不通过校验   抛出异常
        value = self.cleaned_data.get('username')
        if models.User.objects.filter(username=value):
            raise ValidationError('用户名已存在')
        return value

    username = forms.CharField(
        min_length=4,
        required=True,
        label="用户名",
        validators=[],
        error_messages={
            "required": "不能为空",
            "invalid": "格式错误",
            "min_length": "用户名最短4位"
        }
    )
    name = forms.CharField(

        required=True,
        label="姓名",
        error_messages={
            "required": "不能为空",

        }
    )
    phone = forms.CharField(

        required=True,
        label="手机号",
        error_messages={
            "required": "不能为空",

        }
    )

def User_add(request):
    if request.method == 'POST':
        form_obj = LoginForm2(request.POST)

        if form_obj.is_valid():  # 校验数据
            # 插入数据库
            print(form_obj.cleaned_data)
            print(request.POST)
            username = form_obj.cleaned_data['username']
            name = form_obj.cleaned_data['name']
            phone = form_obj.cleaned_data['phone']
            models.User.objects.create(username=username,name=name,phone=phone)

            return redirect('/User_list/')
    else:
        form_obj = LoginForm2()
    return render(request, 'User_add.html', {'form_obj': form_obj})


def User_del(request):

    id = request.GET.get('id')

    models.User.objects.filter(pk=id).delete()

    return HttpResponse(1)